package erp_services

import (
	"xiehui-service/app/models/erp_models"
	"xiehui-service/global"
)

type materialStockService struct {
}

var MaterialStockService = new(materialStockService)

// 新增
func (materialStockService *materialStockService) Create(ms erp_models.MaterialStock) (uint, error) {
	result := global.App.DB.Create(&ms)
	if result.Error != nil {
		return 0, result.Error
	}
	return ms.ID.ID, nil
}

// 更新
func (materialStockService *materialStockService) Update(ms erp_models.MaterialStock) (err error) {
	if err := global.App.DB.Model(&erp_models.MaterialStock{}).Where("id = ?", ms.ID.ID).Omit("id", "created_at").Updates(&ms).Error; err != nil {
		return err
	}
	return nil
}

// 根据拌台号查询仓库
func (materialStockService *materialStockService) GetStockByMixerMaterial(line_number string, material string) (stock *erp_models.Stock, err error) {
	var ms erp_models.MaterialStock
	if err = global.App.DB.Where("line_number = ?", line_number).Where("material_name = ?", material).First(&ms).Error; err != nil {
		return nil, err
	}
	stock, err = StockService.GetStockById(ms.StockId, ms.MixingStationId)
	if err != nil {
		return nil, err
	}
	return stock, nil
}
